package com.shunya.diskorganizer;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.EventObject;

public class SearchWindow extends JPanel {
    private static JFrame frame;
    private JTextField searchTextField;
    private JTable searchResultTable;

    public SearchWindow() throws ClassNotFoundException {
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        searchTextField = new JTextField(20);
        searchTextField.setFont(new Font("Arial", Font.TRUETYPE_FONT, 14));
        searchTextField.setPreferredSize(new Dimension(80, 35));
        searchTextField.getDocument().addDocumentListener(
                new DocumentListener() {
                    public void changedUpdate(DocumentEvent e) {
//                        updateSearchResult();
                    }

                    public void insertUpdate(DocumentEvent e) {
//                        updateSearchResult();
                    }

                    public void removeUpdate(DocumentEvent e) {
//                        updateSearchResult();
                    }
                });
        searchResultTable = new JTable(new DefaultTableModel(new String[]{"#", "Document", "Category"}, 10));
//		 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        searchResultTable.setShowGrid(false);
//        searchResultTable.setPreferredScrollableViewportSize(new Dimension(800, 700));
        searchResultTable.setFillsViewportHeight(true);
        searchResultTable.setAutoCreateRowSorter(true);
        searchResultTable.setRowHeight(40);
        searchResultTable.setRowMargin(0);
        searchResultTable.setDragEnabled(true);
        searchResultTable.setIntercellSpacing(new Dimension(0, 0));
        searchResultTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        searchResultTable.setFont(new Font("Arial", Font.TRUETYPE_FONT, 11));
        TableCellRenderer dcr = searchResultTable.getDefaultRenderer(String.class);
        if (dcr instanceof JLabel) {
            ((JLabel) dcr).setVerticalAlignment(SwingConstants.TOP);
            ((JLabel) dcr).setBorder(new EmptyBorder(0, 0, 0, 0));
        }
        JTableHeader header = searchResultTable.getTableHeader();
        TableCellRenderer headerRenderer = header.getDefaultRenderer();
        if (headerRenderer instanceof JLabel) {
            ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
        }
        header.setPreferredSize(new Dimension(30, 15));

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        c.gridx = 0;
        c.gridy = 0;
        add(searchTextField, c);

        c.fill = GridBagConstraints.BOTH;
        c.ipady = 0;      //make this component tall
        c.weightx = 0.0;
        c.weighty = 0.9;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 1;
        add(new JScrollPane(searchResultTable), c);
    }


    private static void createAndShowGUI() throws Exception {
        //Create and set up the window.
        frame = new JFrame("Search Me..");
        JFrame.setDefaultLookAndFeelDecorated(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        SearchWindow newContentPane = new SearchWindow();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                    createAndShowGUI();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}